package com.hqu.ms.person;

import com.hqu.ms.db.FavDB;
import com.hqu.ms.db.FavDBDao;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;

public class FavListAdapter extends CursorAdapter {

	private PersonFragment mFragment;
	private FavDBDao mFavDBDao;

	public FavListAdapter(Context context, Cursor c, FavDBDao dao,
			PersonFragment fragment) {
		super(context, c, true);
		mFragment = fragment;
		mFavDBDao = dao;
	}

	private boolean mShowDelete = false;

	public void setShowDelete(boolean isShow) {
		mShowDelete = isShow;
	}

	private View.OnClickListener deleteClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			mFragment.delete((FavDB) v.getTag());
		}
	};

	private void bind(FavItemVIew view, Cursor cursor) {
		view.bindItem(mFavDBDao.readEntity(cursor, 0), mShowDelete,
				deleteClickListener);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		if (view != null && view instanceof FavItemVIew) {
			bind(((FavItemVIew) view), cursor);
		}
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup group) {
		FavItemVIew itemView = FavItemVIew_.build(context);
		bind(itemView, cursor);
		return itemView;
	}

}
